<?php
namespace app\distribution\model;
use app\BaseModel;
use app\member\model\AccountLogModel;
use think\facade\Cache;
use think\Db;
//*------------------------------------------------------ */
//-- 分销身份表
/*------------------------------------------------------ */
class DividendAwardModel extends BaseModel
{
	protected $table = 'distribution_dividend_award';
	public  $pk = 'award_id';
	protected static $mkey = 'distribution_dividend_award_list';
	
	 /*------------------------------------------------------ */
	//-- 清除缓存
	/*------------------------------------------------------ */ 
	public function cleanMemcache(){
		Cache::rm(self::$mkey);
	}
	/*------------------------------------------------------ */
	//-- 获取列表
	/*------------------------------------------------------ */ 
	public  function getRows(){
		$data = Cache::get(self::$mkey);
		if (empty($data) == false){
			return $data;
		}
		$rows = $this->field('*,award_id as id,award_name as name')->order('award_id ASC')->select()->toArray();		
		foreach ($rows as $row){
			$data[$row['role_id']] = $row;
		}
		Cache::set(self::$mkey,$data,600);
		return $data;
	}
	/*------------------------------------------------------ */
	//-- 获取角色信息
	/*------------------------------------------------------ */ 
	public function info($award_id,$returnName = false){
		$rows = $this->getRows();
		if ($returnName == true){
			return $rows[$award_id]['award_name'];
		}
		return $rows[$award_id];
	}

	// 团队奖
	public function td_award($order_id){
		$order = Db::table('shop_order_info')->where('order_id',$order_id)->find();
		if(empty($order)) return;
		$goods_id = explode(',',$order['buy_goods_id']);
		$thisuserid = Db::table('users')->where('user_id',$order['user_id'])->value('pid');
		if($thisuserid){
			$AccountLogModel = new AccountLogModel();
		
			foreach($goods_id as $vgood_id){
				//查询奖励比例
				$td_award_set = Db::table('shop_goods')->where('goods_id',$vgood_id)->value('td_award');
				$order_goods = Db::table('shop_order_goods')->where('order_id',$order_id)->where('goods_id',$vgood_id)->where('is_dividend',1)->find();
				$goodsmoney = $order_goods['shop_price']*$order_goods['goods_number'];
				$td_award_set = (array)json_decode($td_award_set);
				$fa_award = 0;
				$fa_role_id = 0;
				while(1){
					//查上级
					$thisuser = Db::table('users')->where('user_id',$thisuserid)->find();
					if(empty($thisuser)) break;
					if($thisuser['role_id'] > $fa_role_id){
						$award = round($goodsmoney*$td_award_set[$thisuser['role_id']]*0.01-$fa_award,2);
						if($award > 0.01){
							//发奖
							$inArr2['status'] = 1;
							$inArr2['order_id'] = $order_id;
							$inArr2['order_sn'] = $order['order_sn'];
							$inArr2['role_id'] = $thisuser['role_id'];
							$inArr2['role_name'] = Db::table('distribution_dividend_role')->where('role_id',$thisuser['role_id'])->value('role_name');
							$buyusernick = Db::table('users')->where('user_id',$order['user_id'])->value('nick_name');
							$inArr2['buy_uid'] = $order['user_id'];
							$inArr2['order_amount'] = $order['goods_amount'];
							$inArr2['dividend_amount'] = $award;
							$inArr2['dividend_uid'] = $thisuser['user_id'];
							$inArr2['award_name'] = '团队收入【'.$buyusernick.'】';
							$inArr2['level_award_name'] = '团队收入';
							$inArr2['award_id'] = 2;
							$inArr2['update_time'] = time();
							$inArr2['add_time'] =  time();
							Db::name('distribution_dividend_log')->insertGetId($inArr2);
							$changedata['change_desc'] = '团队收入【'.$buyusernick.'】';
							$changedata['change_type'] = 4;
							$changedata['by_id'] = $order_id;
							$changedata['total_dividend'] = $award; 
							$AccountLogModel->change($changedata, $thisuser['user_id'], false);

							//调用平级奖
							if($thisuser['pid']){
								$this->pj_award($thisuser['pid'],$award,$thisuser['role_id'],$order_id);
							}	
						}
						$fa_award = $td_award_set[$thisuser['role_id']];
						$fa_role_id = $thisuser['role_id'];
						if($fa_role_id == count($td_award_set)) break;
					}
					$thisuserid = $thisuser['pid'];
			
				}
			}
		}
	
	}

	//直推奖
	public function zt_award($order_id){
		$order = Db::table('shop_order_info')->where('order_id',$order_id)->find();
		if(empty($order)) return;
		//查询奖励比例
		$goods_id = explode(',',$order['buy_goods_id']);
		$thisuserid = Db::table('users')->where('user_id',$order['user_id'])->value('pid');
		if($thisuserid){
			foreach($goods_id as $vgood_id){
				$zt_award_set = Db::table('shop_goods')->where('goods_id',$vgood_id)->where('is_dividend',1)->value('zt_award');
				$order_goods = Db::table('shop_order_goods')->where('order_id',$order_id)->where('goods_id',$vgood_id)->where('is_dividend',1)->find();
				if($zt_award_set && $order_goods){
					$AccountLogModel = new AccountLogModel();
					$thisuser = Db::table('users')->where('user_id',$thisuserid)->find();
		
					if($thisuser && $thisuser['role_id'] > 0){
						$zt_award_set = (array)json_decode($zt_award_set);
						// 商品支付金额
						$goodsmoney = $order_goods['shop_price']*$order_goods['goods_number'];
						$award = round($goodsmoney*$zt_award_set[$thisuser['role_id']]*0.01,2);
						if($award > 0.01 ){
							//发奖
							$inArr2['status'] = 1;
							$inArr2['order_id'] = $order_id;
							$inArr2['order_sn'] = $order['order_sn'];
							$inArr2['buy_uid'] = $order['user_id'];
							$inArr2['role_id'] = $thisuser['role_id'];
							$inArr2['role_name'] = Db::table('distribution_dividend_role')->where('role_id',$thisuser['role_id'])->value('role_name');
							$buyusernick = Db::table('users')->where('user_id',$order['user_id'])->value('nick_name');
							$inArr2['order_amount'] = $order['goods_amount'];
							$inArr2['dividend_amount'] = $award;
							$inArr2['dividend_uid'] = $thisuser['user_id'];
							$inArr2['award_name'] = '直推收入【'.$buyusernick.'】';
							$inArr2['award_id'] = 1;
							$inArr2['level_award_name'] = '直推收入';
							$inArr2['update_time'] = time();
							$inArr2['add_time'] =  time();
							Db::name('distribution_dividend_log')->insertGetId($inArr2);
							// var_dump($inArr2);die;
							$changedata['change_desc'] = '直推收入【'.$buyusernick.'】';
							$changedata['change_type'] = 4;
							$changedata['by_id'] = $order_id;
							$changedata['total_dividend'] = $award; 
							$AccountLogModel->change($changedata, $thisuser['user_id'], false);
							//调用平级奖
							if($thisuser['pid']){
								$this->pj_award($thisuser['pid'],$award,$thisuser['role_id'],$order_id);
							}
						}
					}
				}
			}
		}
		
	}

	//平级奖
	public function pj_award($user_id,$award,$role_id,$order_id){
		//查询奖励比例
		$order = Db::table('shop_order_info')->where('order_id',$order_id)->find();
		$pj_award_set = Db::table('distribution_dividend_award')->where('award_id',3)->find();
		$award_set = (array)json_decode($pj_award_set['award_value']);
		$fa_num = 1;
		$AccountLogModel = new AccountLogModel();
		$thisuserid = $user_id;
		while(1){
			$thisuser = Db::table('users')->where('user_id',$thisuserid)->find();
			if(empty($thisuser)) return;
			if(in_array($thisuser['role_id'],explode(',',$pj_award_set['limit_role'])) and $role_id == $thisuser['role_id']){
				//发奖
				foreach($award_set as $k=>$award_setv){
					if($k == $fa_num){
						if($award_setv->num_type == 'per'){
							$award = round($award_setv->num*$award*0.01,2); 
						}else{
							$award = $award_setv->num; 
						}
						if($award > 0.01){
							//发奖
							$inArr2['status'] = 1;
							$inArr2['order_id'] = $order_id;
							$inArr2['order_sn'] = $order['order_sn'];
							$inArr2['buy_uid'] = $order['user_id'];
							$inArr2['role_id'] = $thisuser['role_id'];
							$inArr2['role_name'] = Db::table('distribution_dividend_role')->where('role_id',$thisuser['role_id'])->value('role_name');
							$buyusernick = Db::table('users')->where('user_id',$order['user_id'])->value('nick_name');
							$inArr2['order_amount'] = $order['goods_amount'];
							$inArr2['dividend_amount'] = $award;
							$inArr2['dividend_uid'] = $thisuser['user_id'];
							$inArr2['award_name'] = '平级收入'.'【'.$buyusernick.'】';
							$inArr2['award_id'] = 3;
							$inArr2['level_award_name'] = '平级收入';
							$inArr2['update_time'] = time();
							$inArr2['add_time'] =  time();
							Db::name('distribution_dividend_log')->insertGetId($inArr2);
							// var_dump($inArr2);
							$changedata['change_desc'] = '平级收入'.'【'.$buyusernick.'】';
							$changedata['change_type'] = 4;
							$changedata['by_id'] = $order_id;
							$changedata['total_dividend'] = $award; 
							$AccountLogModel->change($changedata, $thisuser['user_id'], false);
						}
						$fa_num++;
						break;
					}
				}
				if($fa_num > count($award_set)){
					return;
				}
			}
			$thisuserid = $thisuser['pid'];
			if(empty($thisuserid)) return;
		}
	}

	//业绩累加
	public function yjup($user_id,$money){
		$upuser = Db::table('users')->where('user_id',$user_id)->find();
		if(empty($upuser)){
			return;
		}
		Db::table('users')->where('user_id',$user_id)->setInc('total_consume',$money);
		$pid = $upuser['pid'];
		while(1){
			if(empty($pid)) return;
			$upuser = Db::table('users')->where('user_id',$pid)->find();
			if(empty($upuser)){
				return;
			}
			Db::table('users')->where('user_id',$pid)->setInc('total_consume_td',$money);
			$pid = $upuser['pid'];
		}
	}
}
